<!DOCTYPE HTML>
<html>
  <head>
    <title>Announcement</title>
        <link type = "image/x-icon" rel="SHORTCUT ICON" href="img/favicon.ico" />	
        <script type="text/javascript" src="jquery/jquery-1.7.1.min.js"></script>     
				<link rel="stylesheet" href="css/style.css" />
        <link rel="stylesheet" href="css/grid-12.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <script type="text/javascript" language="javascript">
      
				</script>
  </head>
  <body>
    <div id="container" class="container_12">
    
    <?php
		include("inc/db.class.php");
		include("class/Product.class.php");
		require("php/header.php");
		include("inc/functions.php");
		$database = new Database();
		$edit = FALSE;
		
		if(!isset($_SESSION['login']) || empty($_SESSION['login']) || !isset($_SESSION['email']) || empty($_SESSION['email']) || !isset($_SESSION['user_role']))
			{
				header("Location: login.php?error=invalid-access");
				exit(0);
			}
		elseif($_SESSION['user_role']==2)
			{
				header("Location: test.php?error=invalid-access");
				exit(0);
			}
			
			if(isset($_GET['id']) and !empty($_GET['id']) and isset($_GET['action']) and $_GET['action']=='update')
			{
				$id = $_GET['id'];
				$edit = TRUE;
				$get_announcement = "select * from announcements where id='$id'";
				$record = $database->get_record($get_announcement);
			}
			if(isset($_GET['id']) and !empty($_GET['id']) and isset($_GET['action']) and $_GET['action']=='delete')
			{
				$id = $_GET['id'];
				$delete = TRUE;
				$get_announcement = "delete from announcements where id='$id'";
				$record = $database->query($get_announcement);
			}
			
		?>
      <div id="line" class="grid_12"></div>
			<div id="" class="grid_12">
				<div class="admin-nav">
					<a href="admin-panel.php" class="admin-nav-item">Dashboard</a> | <a href="admin-events.php" class="admin-nav-item">Event</a> | <a href="admin-announcements.php" class="admin-nav-item">Announcements</a>
				</div>
				<h3>Edit Announcements</h3>
			    <div id="contact_us_box" class="">
				<div id=''>
				    <form id="formContactUs" action='proc/addUpdateAnnouncement.php' method='post'>
					<!--Hidden Input Phone number -->
					<div id="contact_us_center">
					    <div class="message center"><?php  if(isset($_GET['m'])){if($_GET['m']=='s') echo "Added."; if($_GET['m']=='f') echo 'Sorry!'; }?></div>
					</div>
					<input id='number' name='usr_nmbr' type='hidden' value='' />
					<div class='inpt_pr_cvr'>
					    <span class='label_contact_us'>Title</span><br />
					    <input id='name' name='title' type='text' class='txt_bx_contact_us' value="<?php if($edit == TRUE) echo $record->title; ?>"  />
					    <span id="name_error" class="error"></span>
					</div>

					<div  class='inpt_pr_cvr'>
					    <span class='label_contact_us'>Description</span><br />
					    <textarea id='message' name='description' class='txt_area_contact_us' maxlength='1000'  ><?php if($edit == TRUE) echo $record->description; ?></textarea><!--onkeyup='return maxlen(this)'-->
					    <br /><span id='message_error' class='error'></span>
					</div>
					<?php 
					if($edit==FALSE)
						echo "<input id='submit_message' name = 'add' type='submit' class='btn2' value='Add' />";
					else
					{	
						echo "<input id='' name = 'id' type='hidden' value='".$id."' />";
						echo "<input id='submit_message' name = 'Update' type='submit' class='btn2' value='Update' />";
					}
					
					?>
					<div class="clear"></div>
				    </form>
				</div>
			    </div>
				<?php
					$get_announcements = "select * from announcements order by modified_at desc";
						foreach($database->get_records($get_announcements) as $announcement){
							$updated_time = date('l jS F Y', strtotime($announcement->modified_at));
							echo '<div class="event-list">
										<a href="admin-announcements.php?id='.$announcement->id.'&action=update" class="floatRight">Edit</a>
										<a href="admin-announcements.php?id='.$announcement->id.'&action=delete" class="floatRight"><img title="Delete" src="img/close.png" /></a>
										<div class="levels"></div><div><h3>'.$announcement->title.'</h3></div>
										<div class="levels"></div><div>'.$announcement->description.'</div>
										<div class="levels">Updated on: </div><div>'.$updated_time.'</div>
									<div class="clear"></div>
								</div>';
							echo '<div class="clear"></div>';
						}
				?>
			</div>			
			<?php require("php/footer.php"); ?>
    </div>
  </body>
</html>
